import {HTTP_CONFIG} from "../config";

class UploadImg {
  chooseImg (count, callback) {
    count = typeof count === 'number' ? count : 1
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album'],
      count,
      success (res) {
        res.tempFilePaths.forEach(imgItem => {
          wx.uploadFile({
            url: HTTP_CONFIG.newUrl+'OnLoad/uploadFile',
            filePath: imgItem,
            name: 'file',
            success (res) {
              let data = JSON.parse(res.data)
              let imgUrl = data.data
              callback && callback(imgUrl)
            },
            fail (err) {
              wx.showToast({
                title: '上传失败，请稍后再试'
              })
            }
          })
        })
      }
    })
  }
}

export default UploadImg
